package com.codi.fcloud.ufx.hssale.portfolio;

import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.ExceptionUtil;
import com.codi.bus.exception.HundsunAPIException;
import com.codi.fcloud.ufx.UfxSDKFactory;
import com.codi.fcloud.ufx.constant.HSInputParameterNames;
import com.codi.fcloud.ufx.constant.UrlConstants;
import com.codi.trade.dto.PortfolioQryDto;
import com.codi.trade.service.portfolio.HsPortfolioQryService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 投资组合查询
 *
 * @author spy
 * @version 1.0 2017-11-06 13:24
 * @since 1.0
 */
@Slf4j
@Service
public class HsPortfolioQryServiceImpl extends UfxSDKFactory implements HsPortfolioQryService {

    @Loggable
    @Override
    public Map<String, Object> query() {
        return query(new PortfolioQryDto());
    }

    @Loggable
    @Override
    public Map<String, Object> query(PortfolioQryDto dto) {

        Preconditions.checkNotNull(dto);

        String trustWay = apiValueMap.get(HSInputParameterNames.TRUST_WAY);
        Map<String, Object> request = new HashMap<>();

        request.put("trust_way", trustWay);
        request.put("net_no", getString(dto.getNet_no(), "0001"));
        request.put("request_num", getString("50", "50")); //	N8	请求行数	Y	最大不能超过50；即最大返回记录条数不会超过50条；
        request.put("reqry_recordsum_flag", getString(dto.getReqry_recordsum_flag(), "1"));//	C1	重新统计总记录数标志	Y	0-不重新统计；1-重新统计；
        request.put("sort_direction", getString(dto.getSort_direction()));//	C1	返回排序方式	N
        request.put("trade_acco", getString(dto.getTrade_acco())); //	C17	交易账号	N
        request.put("ta_acco", getString(dto.getTa_acco()));//	C12	TA账号	N
        request.put("client_id", getString(dto.getClient_id()));//	C18	客户编号	N
        // 交易账号与客户编号至少有一个为必填项 spy 2017-11-6 13:29:02 (针对组合理财的)

        //	N8	查询起始行号	N	比如总记录是100，每50条分页；那么需要查询2次；第1次查询请求行数传50，重新统计总记录数传1，查询起始行号private String 传1；第2次查询请求行数传50，重新统计总记录数传0，查询起始行号传51；
        request.put("qry_beginrownum", getString(dto.getQry_beginrownum()));


        try {
            List<Map<String, Object>> response = getUfxSDK().query(getTarget(), request);
            // 如果requestNum是我们系统定义的最大值，则表明数据需要全部取出来
            if (dto.getRequest_num() != null){
                getNextResponse(request, response, dto.getRequest_num());
            }
            return appendLog(request, response);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, getTarget(), exception);
            throw new HundsunAPIException(ExceptionUtil.getMessage(exception));
        }
    }

    /**
     * 设置入参模型
     *
     * @param requestNum
     * @param queryflag
     * @param sortDirection
     * @param tradeAccount
     * @param taAccount
     * @param clientId
     * @param beginNum
     * @return
     */
    @Override
    public PortfolioQryDto getRequestDto(Integer requestNum, String queryflag, String sortDirection, String tradeAccount, String taAccount, String clientId, String beginNum) {
        PortfolioQryDto portfolioQryDto = new PortfolioQryDto();
        portfolioQryDto.setRequest_num(requestNum);
        portfolioQryDto.setReqry_recordsum_flag(queryflag);
        portfolioQryDto.setSort_direction(sortDirection);
        portfolioQryDto.setTrade_acco(tradeAccount);
        portfolioQryDto.setTa_acco(taAccount);
        portfolioQryDto.setClient_id(clientId);
        portfolioQryDto.setQry_beginrownum(beginNum);
        return portfolioQryDto;
    }

    @Override
    protected String getTarget() {
        return UrlConstants.PREFIX_SALE + UrlConstants.PORTFOLIO_QRY;
    }
}
